package com.maddox.mworld.ui.concat;

import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.maddox.mworld.R;
import com.maddox.relax.util.CharacterParser;
import com.maddox.relax.view.HoverItemDecoration;
import com.maddox.relax.view.SlideBar;

import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * 联系人列表
 */
public class ConcatActivity extends AppCompatActivity {

    private SlideBar slideBar;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;

    private List<ConcatItem> mList = new ArrayList<>();
    private FoodAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_concat);
        initView();
    }

    private void initView() {
        slideBar = (SlideBar) findViewById(R.id.slideBar);
        recyclerView = findViewById(R.id.recyclerview);
        //右边字母监听
        slideBar.setTouchLetterListener(new SlideBar.OnTouchingLetterListener() {
            @Override
            public void onTouchingLetterChanged(String letter) {
                Log.d("Maddox", "选中了:" + letter);
                int postion = getPositionForSelect(letter);
                if (postion!=-1){
                    layoutManager.scrollToPositionWithOffset(postion,0);
                    layoutManager.setStackFromEnd(false);
                }

            }
        });

        adapter = new FoodAdapter(R.layout.item_concat_child_list);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        //自定义的ItemDecoration
        HoverItemDecoration hoverItemDecoration = new HoverItemDecoration(this);
        hoverItemDecoration.setBindItemTextCallback(new HoverItemDecoration.BindItemTextCallback() {
            @Override
            public String getItemText(int position) {
                return mList.get(position).firstSpell;
            }
        });
        recyclerView.addItemDecoration(hoverItemDecoration);
        recyclerView.setAdapter(adapter);

        initTempData();

    }


    //获取右侧栏点击字母对应列表实体类的位置
    private int getPositionForSelect(String letter){
        for (int i = 0; i < mList.size(); i++) {
            String firstSpell = mList.get(i).firstSpell;
            if (letter.equals(firstSpell)){
                return i;
            }
        }
        return -1;

    }

    //初始化假数据
    private void initTempData() {
        String jsonData =  "[{\"firstSpell\":\"A\",\"foodListVOS\":[]},{\"firstSpell\":\"B\",\"foodListVOS\":[{\"firstSpell\":\"B\",\"kid\":448511820603488,\"chName\":\"布朗\"},{\"firstSpell\":\"B\",\"kid\":448515561922657,\"chName\":\"白金瓜\"}]},{\"firstSpell\":\"C\",\"foodListVOS\":[]},{\"firstSpell\":\"D\",\"foodListVOS\":[{\"firstSpell\":\"D\",\"kid\":448509681508457,\"chName\":\"冬果梨\"},{\"firstSpell\":\"D\",\"kid\":448512281976928,\"chName\":\"冬枣\"}]},{\"firstSpell\":\"E\",\"foodListVOS\":[]},{\"firstSpell\":\"F\",\"foodListVOS\":[]},{\"firstSpell\":\"G\",\"foodListVOS\":[{\"firstSpell\":\"G\",\"kid\":448514429460576,\"chName\":\"桂圆\"}]},{\"firstSpell\":\"H\",\"foodListVOS\":[{\"firstSpell\":\"H\",\"kid\":448515696140391,\"chName\":\"黄河蜜瓜\"},{\"firstSpell\":\"H\",\"kid\":448514546901088,\"chName\":\"黄皮果\"},{\"firstSpell\":\"H\",\"kid\":448515645808736,\"chName\":\"哈密瓜\"}]},{\"firstSpell\":\"I\",\"foodListVOS\":[]},{\"firstSpell\":\"J\",\"foodListVOS\":[{\"firstSpell\":\"J\",\"kid\":448509790560361,\"chName\":\"锦丰梨\"},{\"firstSpell\":\"J\",\"kid\":448509832503401,\"chName\":\"京白梨\"},{\"firstSpell\":\"J\",\"kid\":448512206479456,\"chName\":\"酒枣\"},{\"firstSpell\":\"J\",\"kid\":448515738083424,\"chName\":\"金塔寺瓜\"}]},{\"firstSpell\":\"K\",\"foodListVOS\":[{\"firstSpell\":\"K\",\"kid\":448509874446336,\"chName\":\"库尔勒香梨\"}]},{\"firstSpell\":\"L\",\"foodListVOS\":[{\"firstSpell\":\"L\",\"kid\":448509908000873,\"chName\":\"莱阳梨\"},{\"firstSpell\":\"L\",\"kid\":448511594111081,\"chName\":\"李子\"},{\"firstSpell\":\"L\",\"kid\":448514588844128,\"chName\":\"荔枝\"},{\"firstSpell\":\"L\",\"kid\":448511636054114,\"chName\":\"李子杏\"},{\"firstSpell\":\"L\",\"kid\":448515176046688,\"chName\":\"榴莲\"}]},{\"firstSpell\":\"M\",\"foodListVOS\":[{\"firstSpell\":\"M\",\"kid\":448512089038944,\"chName\":\"密云小枣\"},{\"firstSpell\":\"M\",\"kid\":448509949943913,\"chName\":\"马蹄黄梨\"},{\"firstSpell\":\"M\",\"kid\":448509983498345,\"chName\":\"明月梨\"},{\"firstSpell\":\"M\",\"kid\":448513045340257,\"chName\":\"磨盘柿\"}]},{\"firstSpell\":\"N\",\"foodListVOS\":[{\"firstSpell\":\"N\",\"kid\":448514093916160,\"chName\":\"柠檬\"}]},{\"firstSpell\":\"O\",\"foodListVOS\":[]},{\"firstSpell\":\"P\",\"foodListVOS\":[{\"firstSpell\":\"P\",\"kid\":448512718184544,\"chName\":\"葡萄干\"},{\"firstSpell\":\"P\",\"kid\":448511350841449,\"chName\":\"蒲桃\"},{\"firstSpell\":\"P\",\"kid\":448514882445408,\"chName\":\"枇杷\"}]},{\"firstSpell\":\"Q\",\"foodListVOS\":[{\"firstSpell\":\"Q\",\"kid\":448511384395879,\"chName\":\"庆丰桃\"},{\"firstSpell\":\"Q\",\"kid\":448509337575528,\"chName\":\"秋里蒙苹果\"}]},{\"firstSpell\":\"R\",\"foodListVOS\":[{\"firstSpell\":\"R\",\"kid\":448514714673249,\"chName\":\"人参果\"}]},{\"firstSpell\":\"S\",\"foodListVOS\":[{\"firstSpell\":\"S\",\"kid\":448510839136352,\"chName\":\"沙果\"},{\"firstSpell\":\"S\",\"kid\":448510981742688,\"chName\":\"蛇果\"},{\"firstSpell\":\"S\",\"kid\":448513364107360,\"chName\":\"沙棘\"},{\"firstSpell\":\"S\",\"kid\":448512357474401,\"chName\":\"酸枣\"},{\"firstSpell\":\"S\",\"kid\":448515394150496,\"chName\":\"山竹\"}]},{\"firstSpell\":\"T\",\"foodListVOS\":[]},{\"firstSpell\":\"U\",\"foodListVOS\":[]},{\"firstSpell\":\"V\",\"foodListVOS\":[]},{\"firstSpell\":\"W\",\"foodListVOS\":[{\"firstSpell\":\"W\",\"kid\":448513397661792,\"chName\":\"无花果\"},{\"firstSpell\":\"W\",\"kid\":448509488570472,\"chName\":\"倭锦苹果\"}]},{\"firstSpell\":\"X\",\"foodListVOS\":[{\"firstSpell\":\"X\",\"kid\":448511711551584,\"chName\":\"杏\"},{\"firstSpell\":\"X\",\"kid\":448511862546528,\"chName\":\"西梅\"},{\"firstSpell\":\"X\",\"kid\":448510335819881,\"chName\":\"雪花梨\"},{\"firstSpell\":\"X\",\"kid\":448510377762921,\"chName\":\"雪梨\"}]},{\"firstSpell\":\"Y\",\"foodListVOS\":[{\"firstSpell\":\"Y\",\"kid\":448509413073000,\"chName\":\"印度苹果\"},{\"firstSpell\":\"Y\",\"kid\":448514798559328,\"chName\":\"杨桃\"},{\"firstSpell\":\"Y\",\"kid\":448514840502368,\"chName\":\"椰子\"}]},{\"firstSpell\":\"Z\",\"foodListVOS\":[{\"firstSpell\":\"Z\",\"kid\":448509639565417,\"chName\":\"长把梨\"}]}]";
        Gson gson = new Gson();
        Type type = new TypeToken<List<DataItem>>() {
        }.getType();
        List<DataItem> dataItems = gson.fromJson(jsonData, type);
        for (DataItem item : dataItems) {
            if (item.foodListVOS.size()>0){
                mList.addAll(item.foodListVOS);
            }
        }
        adapter.setNewData(mList);

    }

    //适配器
    class FoodAdapter extends BaseQuickAdapter<ConcatItem, BaseViewHolder> {

        public FoodAdapter(int layoutResId) {
            super(layoutResId);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder holder, ConcatItem s) {
            holder.setText(R.id.tv_title_name, s.chName);
        }
    }
}